package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private JedisPool jedisPool;

    private Logger logger = LoggerFactory.getLogger(SetmealServiceImpl.class);

    @Override
    public List<CheckItem> findAll() {
        return checkGroupDao.findAll();
    }

    /**
     * 添加检查组
     *
     * @param checkGroup
     * @param checkitemIds 后端:
     *                     1,创建CheckGroupController接收数据,通过CheckGroup , checkitemId 接收,
     *                     2,创建CheckGroupService,调用服务 调用dao添加检查组CheckGroup,
     *                     添加关联CheckGroup.getId(),checkitemld向t_checkgroup_checkitem添加关联
     */
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                checkGroupDao.setCheckGroupAndCheckItem(checkGroupId, checkitemId);
            }
        }
        jedisPool.getResource().flushDB();
        logger.debug("添加检查组操作后清空redis");
    }

    @Override
    public PageInfo<CheckGroup> findPage(QueryPageBean queryPageBean) {
        //判断输入框的内容是否不为空
        if (!StringUtils.isEmpty(queryPageBean.getQueryString())) {
            queryPageBean.setQueryString("%" + queryPageBean.getQueryString() + "%");
        }
        //使用分页插件pageHelper
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<CheckGroup> checkGroups = checkGroupDao.findPage(queryPageBean.getQueryString());
        PageInfo<CheckGroup> checkGroupPageInfo = new PageInfo<>(checkGroups);
        return checkGroupPageInfo;
    }

    @Override
    public List<Integer> findCheckGroupById(Integer checkgroupId) {
        return checkGroupDao.findCheckGroupById(checkgroupId);
    }

    /**
     * @param
     * @param checkGroup
     * @param checkitemIds
     * @return void
     * @author wu_zh
     * @date 2019/7/1 15:30
     * @Description 更新检查组
     */
    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        Integer checkGroupId = checkGroup.getId();

        /**
         * 删除表关系
         */
        checkGroupDao.deleteCheckGroupAndCheckitem(checkGroupId);

        /**
         * 向中间表插入数据
         */
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                checkGroupDao.setCheckGroupAndCheckItem(checkGroupId, checkitemId);
            }
        }
        checkGroupDao.update(checkGroup);

        jedisPool.getResource().flushDB();
        logger.debug("更新检查组操作后清空redis");

    }

    /**
     * @param
     * @param id
     * @return void
     * @author wu_zh
     * @date 2019/7/1 15:30
     * @Description 删除检查组
     */
    @Override
    public void deleteById(Integer id) {
        checkGroupDao.deleteByIdAndRelations(id);
        checkGroupDao.deleteById(id);
        jedisPool.getResource().flushDB();
        logger.debug("删除检查组操作后清空redis");
    }
}
